<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 检测是否为2-4位汉字


    function testReg() {
      var hanzi = prompt("请输入：");
      var reg = /^[\u4e00-\u9fa5]{2,4}$/;
      if (reg.test(hanzi)) {
        alert("是2-4位的汉字");
      } else {
        alert("不是2-4位的汉字");
      }
    }
    testReg();
  </script>
</body>

</html>